package leetcode;


import java.util.Stack;

/**
 * 逆波兰表达式求值
 */
public class LeetCode150 {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (String token : tokens) {
            switch (token) {
                case "+": {
                    int n1 = stack.pop();
                    int n2 = stack.pop();
                    stack.push((n2 + n1));
                    break;
                }
                case "-": {
                    int n1 = stack.pop();
                    int n2 = stack.pop();
                    stack.push((n2 - n1));
                    break;
                }
                case "*": {
                    int n1 = stack.pop();
                    int n2 = stack.pop();
                    stack.push((n2 * n1));
                    break;
                }
                case "/": {
                    int n1 = stack.pop();
                    int n2 = stack.pop();
                    stack.push((n2 / n1));
                    break;
                }
                default:
                    stack.push(Integer.parseInt(token));
                    break;
            }
        }
        return stack.pop();
    }

    public static void main(String[] args) {
        LeetCode150 leetCode150 = new LeetCode150();
        System.out.println(leetCode150.evalRPN(new String[]{"2", "1", "+", "3", "*"}));
    }
}